//
//  CityDynamicViewController.m
//  cityStar
//
//  Created by wangjia on 14/12/13.
//  Copyright (c) 2014年 tiny. All rights reserved.
//

#import "CityDynamicViewController.h"
#import "UIScrollView+SVPullToRefresh.h"
#import "UIScrollView+SVInfiniteScrolling.h"
#import "Defines.h"
#import "KInterFace.h"
#import "HttpRequestManager.h"
#import "NewsList.h"
#import "NewListCell.h"
#import "NewsBanner.h"
#import "CityDynamicDetailViewController.h"

@interface CityDynamicViewController (){
    NSMutableArray *maryContent;
}

@property (nonatomic,assign) int page;

@end

@implementation CityDynamicViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.sectionFooterHeight = 0;
    self.tableView.sectionHeaderHeight = 0;
    
    [self loadBanner];
    
    __weak typeof(self)weakSelf = self;
    
    maryContent = [[NSMutableArray alloc]init];
    [self.tableView addPullToRefreshWithActionHandler:^{
        weakSelf.page = 1;
        [weakSelf webRequest_GetVideoMore:NO];
    }];
    [self.tableView addInfiniteScrollingWithActionHandler:^{
        [weakSelf webRequest_GetVideoMore:YES];
    }];
    [self.tableView triggerPullToRefresh];
    
    //分类 http://api.icszx.com/news/category
    //banner http://api.icszx.com/banner/show/0/122
    
//    全球 查询对应category = 0
//    财经 查询对应category = 1
//    股票 查询对应category = 2
//    城市新闻 查询对应category = 3
//    城市民生 查询对应category = 4
//    城市315 查询对应category = 5
//    娱乐 查询对应category = 6
//    体育 查询对应category = 7
//    军事 查询对应category = 8
//    科技 查询对应category = 9
//    房产 查询对应category = 10
    
    //分页 http://api.icszx.com/news/lists/city/109/category/1
    //详情 http://api.icszx.com/news/view/5509066db3a3ce910d953091
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)loadBanner{
    NSString *strUrl = [@"" stringByAppendingFormat:@"%@/%d/%@",KinterFace_bannerShow,self.intID,@"122"];
    
    [HttpRequestManager getMethod:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            NSArray *aryBanner = [NewsBanner getListArray:response[@"value"]];
            if (aryBanner.count > 0) {
                self.tableView.tableHeaderView = self.vHeader;
                self.vHeader.aryImage = aryBanner;
            }
        }
    }];
}

- (void)webRequest_GetVideoMore:(BOOL)isMore
{
    
    NSString *strUrl = [@"" stringByAppendingFormat:@"%@/city/%d/category/%d/page/%d",KinterFace_NewsLists,122,self.intID,isMore?self.page:0];
    
    [HttpRequestManager getMethod:[strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding] parameters:nil completedMainBlock:^(BOOL success, id response, NSString *errMsg) {
        if (success) {
            DLog(@"%@",response);
            if (isMore) {
                [maryContent addObjectsFromArray:[NewsList getListArray:response[@"value"][@"content"]]];
                self.page = self.page + 1;
            }else{
                maryContent = [[NSMutableArray alloc]initWithArray:[NewsList getListArray:response[@"value"][@"content"]]];
            }
            [self.tableView reloadData];
//            if (maryContent.count == 0) {
//                self.nothinglabel.hidden = NO;
//            }else{
//                self.nothinglabel.hidden = YES;
//            }
        }
        if (isMore) {
            [self.tableView.infiniteScrollingView stopAnimating];
        }else{
            [self.tableView.pullToRefreshView stopAnimating];
        }
    }];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 1) {
        return 10;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIdentifier = @"CellNewsList";
    NewListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    NewsList *newList = maryContent[indexPath.row];
    [cell setValueWithObject:newList];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return maryContent.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NewsList *newList = maryContent[indexPath.row];
    [self performSegueWithIdentifier:@"segDetail" sender:newList];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.1;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [UIView new];
}

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segDetail"]) {
        NewsList *newList = sender;
        CityDynamicDetailViewController *viewController = segue.destinationViewController;
        viewController.strID = newList._id;
    }
}


@end
